package com.qf.controller;

import com.qf.enums.InterfaceExceptionEnums;
import com.qf.form.SmsInterfaceForm;
import com.qf.service.SmsService;
import com.qf.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/19 22:35
 * description:
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class SMSInterfaceController {

    @Autowired
    SmsService smsService;

    //获取yml中的ip-header的数据，并存入到list集合中
    @Value("${ip-header}")
    private List<String> ipHeader;

    @RequestMapping("/smsInterface")
    public Result smsInterface(SmsInterfaceForm interfaceForm, HttpServletRequest request){
        String ip=this.getRealIp(request);
        log.info("[接口模块]  ip={}",ip);
        interfaceForm.setIp(ip);
        //数据校验（判断是否为空）
        Result result=this.checkData(interfaceForm);
        if(result!=null){
            return result;
        }
        //继续操作
        result=smsService.sendSMS(interfaceForm);

        return result;

    }

    private Result checkData(SmsInterfaceForm interfaceForm) {
        String clientID = interfaceForm.getClientID();
        //如果客户端id为空 返回前端
        if(StringUtils.isEmpty(clientID)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_CLIENT_ID_ERROR);
        }
        String content = interfaceForm.getContent();
        if(StringUtils.isEmpty(content)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_CONTENT_ERROR);
        }
        String ip = interfaceForm.getIp();
        if(StringUtils.isEmpty(ip)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_IP_ERROR);
        }
        String mobile = interfaceForm.getMobile();
        if(StringUtils.isEmpty(mobile)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_MOBILE_ERROR);
        }
        String pwd = interfaceForm.getPwd();
        if(StringUtils.isEmpty(pwd)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_PWD_ERROR);
        }
        String srcID = interfaceForm.getSrcID();
        if(StringUtils.isEmpty(srcID)){
            return Result.error(InterfaceExceptionEnums.INTERFACE_SRC_ID_ERROR);
        }
        return null;
    }

    //获取真实的ip
    private String getRealIp(HttpServletRequest request) {

        String ip=null;
        //如果集合是否为空
        if(!CollectionUtils.isEmpty(ipHeader)){
            //不为空，则循环ipHeader字段，获取真实IP地址
            for (String key : ipHeader) {
                ip = request.getHeader(key);
                if(!StringUtils.isEmpty(ip)){
                    return ip;

                }
            }
        }
        if(StringUtils.isEmpty(ip)){
            ip = request.getRemoteAddr();
        }
        return ip;

    }
}
